package com.xiao.gulimall.product.service.impl;

import com.xiao.gulimall.product.entity.SkuImagesEntity;
import com.xiao.gulimall.product.entity.SpuInfoDescEntity;
import com.xiao.gulimall.product.entity.SpuInfoEntity;
import com.xiao.gulimall.product.service.*;
import com.xiao.gulimall.product.vo.SkuItemSaleAttrVo;
import com.xiao.gulimall.product.vo.SkuItemVo;
import com.xiao.gulimall.product.vo.SpuItemAttrGroupVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.common.utils.PageUtils;
import com.xiao.common.utils.Query;

import com.xiao.gulimall.product.dao.SkuInfoDao;
import com.xiao.gulimall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuSaleAttrValueService saleAttrValueService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public SkuItemVo item(Long skuId) {
        //需要进行Skuitemvo的封装
        SkuItemVo skuItemVo = new SkuItemVo();
        //基本信息获取
        SkuInfoEntity info = skuInfoDao.selectById(skuId);
        skuItemVo.setInfo(info);
        //获取spuid
        Long spuId = info.getSpuId();
        //获取catalogID
        Long catalogId = info.getCatalogId();
        //获取sku的图片信息
        List<SkuImagesEntity> imagesEntities = skuImagesService.list();
        skuItemVo.setImages(imagesEntities);
        //获取spu的销售属性
        List<SkuItemSaleAttrVo> skuItemSaleAttrVos = saleAttrValueService.listSaleAttrs(spuId);
        skuItemVo.setSaleAttr(skuItemSaleAttrVos);
        SpuInfoDescEntity descEntity = spuInfoDescService.getById(spuId);
        skuItemVo.setDesc(descEntity);
        SpuInfoEntity spuInfoServiceById = spuInfoService.getById(spuId);
        skuItemVo.setSpuInfoDescEntity(spuInfoServiceById);
        List<SpuItemAttrGroupVo> attrValuesById = productAttrValueService.getAttrValuesById(spuId, catalogId);
        skuItemVo.setGroupAttrs(attrValuesById);
        return skuItemVo;
    }

}